package com.heima.wemedia.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.media.dtos.SensitiveResponseDTO;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.pojo.WmSensitive;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Override
    public List<String> getSensitiveWord() {
        List<WmSensitive> sensitiveList = this.list();
        if (CollectionUtils.isNotEmpty(sensitiveList)) {
            return sensitiveList.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * 获取敏感词列表
     *
     * @return
     */
    @Override
    public Page<SensitiveResponseDTO> findListByPage(SensitiveDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Page<WmSensitive> ipage = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        // 根据敏感词进行模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(dto.getSensitives()), WmSensitive::getSensitives, dto.getSensitives());
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        this.page(ipage, queryWrapper);

        List<WmSensitive> wmSensitiveList = ipage.getRecords();
        if (CollectionUtils.isEmpty(wmSensitiveList)) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Page<SensitiveResponseDTO> responsePage = new Page<>();
        responsePage.setTotal(ipage.getTotal());
        responsePage.setRecords(wmSensitiveList.stream().map(this::toSensitiveResponseDTO).collect(Collectors.toList()));
        return responsePage;
    }

    /**
     * 将WmSensitive 转为 SensitiveResponseDTO
     *
     * @param wmSensitive
     * @return
     */
    private SensitiveResponseDTO toSensitiveResponseDTO(WmSensitive wmSensitive) {
        SensitiveResponseDTO dto = new SensitiveResponseDTO();
        dto.setId(wmSensitive.getId());
        dto.setSensitives(wmSensitive.getSensitives());
        dto.setCreatedTime(wmSensitive.getCreatedTime());
        return dto;
    }

    /**
     * 保存敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public void saveSensitive(SensitiveDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        sensitivesDuplicateCheck(dto.getSensitives());
        this.save(BeanHelper.copyProperties(dto, WmSensitive.class));

    }

    /**
     * 检查敏感词是否重复
     *
     * @param sensitives
     */
    private void sensitivesDuplicateCheck(String sensitives) {
        if (StringUtils.isEmpty(sensitives)) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断敏感词是否存在
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives, sensitives);
        WmSensitive wmSensitiveDb = this.getOne(queryWrapper);
        // 敏感词已存在,则不允许新增
        if (wmSensitiveDb != null) {
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
        }
    }


    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public void updateSensitive(SensitiveDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 检查敏感词是否重复
        this.sensitivesDuplicateCheck(dto.getSensitives());

        /*根据id查询数据库中*/
        WmSensitive sensitiveDB = this.getById(dto.getId());
        if (sensitiveDB == null) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        sensitiveDB.setSensitives(dto.getSensitives());
        sensitiveDB.setCreatedTime(new Date());
        /*修改数据*/
        this.updateById(sensitiveDB);

    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public void deleteSensitive(Integer id) {
        if (id == null || id < 0) {
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        /*根据id查询*/
        WmSensitive sensitiveDB = this.getById(id);

        // 如果查询不到Sensitive信息 抛出异常,并记录错误日志
        if (sensitiveDB == null) {
            log.error("无法根据wm_Sensitive_id 查询到 Sensitive信息 id: " + id);
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        this.removeById(id);
    }

}
